import mxpGetSysInfoStore from '@/mxp-ui/mxp-tool/store/mxp-store-sys-info.ts';

// #ifdef MP-KUAISHOU
import { type KsMpConfig, ksMpConfig } from '@/config/mp/ks.ts';
// #endif

// #ifdef MP-TOUTIAO
import { type TtMpConfig, ttMpConfig } from '@/config/mp/tt.ts';
// #endif

// #ifdef MP-WEIXIN
import { type WxMpConfig, wxMpConfig } from '@/config/mp/wx.ts';
// #endif

/** 得到appid */
export function getAppId(): string {
  let appid = '';
  // #ifdef MP-KUAISHOU
  const sys = mxpGetSysInfoStore().getSysInfo();
  const isDevtools = sys.value.platform === 'devtools';
  if (isDevtools) {
    // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
    const res = require('./getProject.js');
    appid = res.rojectConfig.appid;
  } else {
    appid = uni.getAccountInfoSync?.()?.miniProgram?.appId;
  }
  // #endif

  // #ifdef MP-TOUTIAO
  appid = tt.getEnvInfoSync?.()?.microapp?.appId;
  // #endif

  // #ifndef MP-TOUTIAO || MP-KUAISHOU
  appid = uni.getAccountInfoSync?.()?.miniProgram?.appId;
  // #endif

  return appid;
}

/** 得到app 信息 */
export function getAppInfo(): KsMpConfig | TtMpConfig | WxMpConfig | undefined {
  const appid = getAppId();
  let appInfo: KsMpConfig | TtMpConfig | WxMpConfig | undefined = undefined;
  // #ifdef MP-KUAISHOU
  appInfo = ksMpConfig[appid as keyof typeof ksMpConfig] || appInfo;
  // #endif
  // #ifdef MP-TOUTIAO
  appInfo = ttMpConfig[appid as keyof typeof ttMpConfig] || appInfo;
  // #endif
  // #ifdef MP-WEIXIN
  appInfo = wxMpConfig[appid as keyof typeof wxMpConfig] || appInfo;
  // #endif
  return appInfo;
}
